'''
Created on Apr 6, 2011

@author: glow
'''

import os
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.api import users

from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden

import xml

from models import CodedTerminology, NCI_Controlled_Terminology_Codelist, \
                    NCI_Controlled_Terminology_CodeListItem, Controlled_Terminology_Source

def index(response):
  return HttpResponse('Hello World')


class MyTemplate(webapp.RequestHandler):
  """
  from http://waoewaoe.wordpress.com/2011/01/09/global-template-system-in-google-app-engine/
  """
  def render(self, template_name, param):
    template_values = dict({'template_name': template_name}, ** param)
    path = os.path.join(os.path.dirname(__file__) + '/view', 'my_template.html')
    self.response.out.write(template.render(path, template_values))

class MainPage(MyTemplate): # extend MyTemplate
  def get(self):
    template_values = {'content' : 'hello world'}
    # calls render function from super class (MyTemplate)
    self.render('index.html', template_values)

def login_required(func):
  """
  Decorator that redirects to the login page if you're not logged in.
  taken from http://code.google.com/p/rietveld/source/browse/trunk/codereview/views.py
  """

  def login_wrapper(request, *args, **kwds):
    if request.user is None:
      return HttpResponseRedirect(
          users.create_login_url(request.get_full_path().encode('utf-8')))
    return func(request, *args, **kwds)

  return login_wrapper

def admin_required(func):
  """
  Decorator that insists that you're logged in as administratior.
  taken from http://code.google.com/p/rietveld/source/browse/trunk/codereview/views.py
  """

  def admin_wrapper(request, *args, **kwds):
    if request.user is None:
      return HttpResponseRedirect(
          users.create_login_url(request.get_full_path().encode('utf-8')))
    if not request.user_is_admin:
      return HttpResponseForbidden('You must be admin in for this function')
    return func(request, *args, **kwds)

  return admin_wrapper
    
   
class UpdateTerminology(webapp.RequestHandler):
  """
  Prompt the System to renew it's CT sources
  - NCI CT
    - http://evs.nci.nih.gov/ftp1/CDISC/SDTM/cdash-terminology.odm.xml
    - http://evs.nci.nih.gov/ftp1/CDISC/SDTM/sdtm-terminology.odm.xml
  - TODO: ADT 
  """
  def get(self):
    pass
  
  def post(self):
    pass
  

def codelist(request):
  pass
